//
//  TDAllDriveMessageShowC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/28.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDAllDriveMessageShowC.h"

#import "TDAllDriveMessageHeadCell.h"
#import "TDAllDriveInfoBasicCell.h"
#import "TDAllDriveInfoImgCell.h"
#import "NetWorkRequest.h"

@interface TDAllDriveMessageShowC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign) NSInteger selIndex;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)UIImage *getImage;//拍照获取到的照片-------

@property (nonatomic,copy)NSString *driveID;
@property (nonatomic,copy)NSString *driveName;
@property (nonatomic,copy)NSString *driveToTime;//到期时间


@property (nonatomic,strong)NSArray *idDetailArray;
@property (nonatomic,strong)NSArray *driveDetailArray;


@end

@implementation TDAllDriveMessageShowC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupUI];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    
}

- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI {
    //设置导航条
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TDAllDriveMessageHeadCell" bundle:nil] forCellReuseIdentifier:@"TDAllDriveMessageHeadCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDAllDriveInfoBasicCell" bundle:nil] forCellReuseIdentifier:@"TDAllDriveInfoBasicCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDAllDriveInfoImgCell" bundle:nil] forCellReuseIdentifier:@"TDAllDriveInfoImgCellId"];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -10, 0, -10)];
    }
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
}



#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 7;
    }else if (section == 1) {
        return 5;
    }
    return 3;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}



- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TDAllDriveInfoBasicCellId";
    if (indexPath.row == 0) {
        iden = @"TDAllDriveMessageHeadCellId";
        return iden;
    }
    else {
        if (indexPath.section == 0 || indexPath.section == 1) {
            if ((indexPath.section ==1 && indexPath.row == 4) || (indexPath.section ==0 && indexPath.row == 6)) {
                iden = @"TDAllDriveInfoImgCellId";
                return iden;
            }else {
                iden = @"TDAllDriveInfoBasicCellId";
                return iden;
            }
        }else {
            iden = @"TDAllDriveInfoBasicCellId";
            return iden;
        }
        
    }
    return iden;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 75;
    }
    else {
        if (indexPath.section == 0 || indexPath.section == 1) {
            if ((indexPath.section ==1 && indexPath.row == 4) || (indexPath.section ==0 && indexPath.row == 6)) {
                return 107;
            }else {
                return 70;
            }
        }else {
            return 70;
        }
        
    }
    return 70;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        TDAllDriveMessageHeadCell *allDriveMessageHeadCell = (TDAllDriveMessageHeadCell *)cell;
        allDriveMessageHeadCell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.section == 0) {
            allDriveMessageHeadCell.lbe_title.text = @"身份证信息";
        }else if (indexPath.section == 1) {
            allDriveMessageHeadCell.lbe_title.text = @"驾驶证信息";
        }else {
            allDriveMessageHeadCell.lbe_title.text = @"住所信息";
        }
    }
    else {
        if (indexPath.section == 0 || indexPath.section == 1) {
            if ((indexPath.section ==1 && indexPath.row == 4) || (indexPath.section ==0 && indexPath.row == 6)) {
                TDAllDriveInfoImgCell *allDriveInfoImgCell = (TDAllDriveInfoImgCell *)cell;
                allDriveInfoImgCell.lbe_desc.text = indexPath.section == 0 ? @"身份证图片" : @"驾驶证图片";
            }else {
                TDAllDriveInfoBasicCell *allDriveInfoBasicCell = (TDAllDriveInfoBasicCell *)cell;
                // MARK: - 代码需要优化  😀一坨翔一样
                if (indexPath.section == 0) {
                    NSArray *titleDesArray = @[@"",@"姓名",@"性别",@"出生年月",@"身份证号码",@"证件有效日至"];
                    
                    allDriveInfoBasicCell.lbe_desc.text = titleDesArray[indexPath.row];
                    if (self.idDetailArray) {
                        allDriveInfoBasicCell.lbe_detail.text = self.idDetailArray[indexPath.row];
                    }
                    
                }else if (indexPath.section == 1) {
                    NSArray *titleDesArray = @[@"",@"姓名",@"驾驶证号码",@"证件有效日至"];
                    
                    allDriveInfoBasicCell.lbe_desc.text = titleDesArray[indexPath.row];
                    if (self.driveDetailArray) {
                        allDriveInfoBasicCell.lbe_detail.text = self.driveDetailArray[indexPath.row];
                    }
                }
            }
        }else {
            TDAllDriveInfoBasicCell *allDriveInfoBasicCell = (TDAllDriveInfoBasicCell *)cell;
            NSArray *titleDesArray = @[@"",@"居住地址",@"房屋状态"];
            allDriveInfoBasicCell.lbe_desc.text = titleDesArray[indexPath.row];
            if (indexPath.row == 1) {
                allDriveInfoBasicCell.lbe_detail.text = _shareMineInfoModel.homeaddress;
            }else {
                allDriveInfoBasicCell.lbe_detail.text = [HelpObject changeNull:_shareMineInfoModel.estatetypenm];
            }
        }
    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



// MARK: - 加载数据
- (void)loadData {
    [self loadNetData];
}

// MARK: - 上传个人信息-------
- (void)loadNetData{
    [self progressHUDShowWithText:nil];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dic = @{};
    [NetWorkRequest postNetRequestData:GetMemberMessage parmeters:dic response:^(NSDictionary *response) {
        [weakSelf progressHUDHideImmediately];
        if (response) {
            //保存数据---进行一系列的操作----------
            [AppSetting shareAppSetting].updatetime = response[@"data"][@"updatetime"];
            weakSelf.shareMineInfoModel = [TBRMineModel yy_modelWithJSON:response[@"data"]];
            
            weakSelf.idDetailArray = @[@"",weakSelf.shareMineInfoModel.membername,[HelpObject changeNull:weakSelf.shareMineInfoModel.sexnm],weakSelf.shareMineInfoModel.birthday,weakSelf.shareMineInfoModel.idno,weakSelf.shareMineInfoModel.idvalidto];
            
            weakSelf.driveDetailArray = @[@"",weakSelf.shareMineInfoModel.membername,[HelpObject changeNull:weakSelf.shareMineInfoModel.driverlicenseno],weakSelf.shareMineInfoModel.licensevalidto];
            
            [weakSelf.tableView reloadData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:@"获取个人信息失败" isSucceed:NO];
    }];
}


@end
